<template>
	<view class="coupon-page">
		<bnn-loading :loading="isLoading"/>
		<scroll-view scroll-y class="coupon-scroll" @scrolltolower="getCouponPage">
			<view class="coupon-list">
				<view v-for="(coupon, key) in dataList" :key="key" class="coupon">
					<view class="coupon-left">
						<image v-if="coupon.status === 0" :src="MyCouponActive" class="coupon-img"/>
						<image v-else :src="MyCoupon" class="coupon-img"/>
						<view class="left-content">
							<view class="price">
								<view class="price-unit">￥</view>
								<view class="price-num">{{coupon.amount}}</view>
							</view>
						</view>
					</view>
					<view class="coupon-right">
						<view class="coupon-name">{{coupon.name}}<span class="only-coupon" v-if="coupon.for_user === 1 && coupon.strategy_name">仅限【{{coupon.strategy_name}}】使用</span></view>
						<view class="h-line"/>
						<view class="time">
							<view class="time-left">
								{{coupon.created_at | parseTime('YYYY/MM/DD')}} - {{coupon.expire_time | parseTime('YYYY/MM/DD')}}
							</view>
							<view v-if="coupon.status === 1" class="time-status">已使用</view>
							<view v-if="coupon.status === 0" class="time-status active">待使用</view>
							<view v-if="coupon.status === -1" class="time-status">已过期</view>
						</view>
					</view>
				</view>
				<view class="list-bottom">
					<view v-if="dataList.length > 0" class="loading-more">
						<load-more :status="loadStatus"/>
					</view>
					<view v-if="dataList.length === 0">
						<!-- <coupon-empty/> -->
						<u-empty text="暂无优惠券" mode="coupon" margin-top="140"></u-empty>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {getCouponPage} from '@/api/trade.js'
	import CouponInfoModel from '@/model/CouponInfoModel.js'
	// import MyCoupon from '@/static/images/my_coupon.png'
	// import MyCouponActive from '@/static/images/my_coupon_active.png'
	import BnnLoading from '@/components/bnn-ui/loading/index.vue'
	import CouponEmpty from '@/components/data-empty/coupon.vue'
	import LoadMore from '@/components/bnn-ui/load-more/index.vue'
	
	export default {
		components: {BnnLoading, LoadMore, CouponEmpty},
		data() {
			return {
				MyCoupon: "https://sou-liang.oss-cn-shanghai.aliyuncs.com/weixin/images/my_coupon.png",
				MyCouponActive: "https://sou-liang.oss-cn-shanghai.aliyuncs.com/weixin/images/my_coupon_active.png",
				isLoading: true,
				page: 1,
				limit: 10,
				loadStatus: 'loading',
				dataList: []
			}
		},
		onLoad() {
			this.getCouponPage()
		},
		methods: {
			//	分页获取优惠券
			getCouponPage () {
				const {dataList, page, limit} = this
				getCouponPage({ page, limit }).then(res => {
					const {data} = res
					this.isLoading = false
					const newData = data.map((coupon, ind) => {
						const Model = new CouponInfoModel()
						const obj = Model.setAttributes(coupon).toObject()
						return obj
					})
					
					this.isLoading = false
					if (data.length) {
						this.dataList = dataList.concat(newData)
						const {ad, Candle} = data
						this.page = page+1
					}
					
					if (data.length==0 || data.length < limit) {
						this.loadStatus = 'noMore'
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.coupon-page{
		.coupon-scroll{
			/* #ifdef APP-PLUS */
			height: 100vh;
			/* #endif */
			
			/* #ifndef APP-PLUS */
			height: calc(100vh - var(--window-top));
			/* #endif */
			background: $theme_bg_color;
		}
		.coupon-list{
			padding: 30rpx;
			.coupon{
				width: 100%;
				height: 170rpx;
				display: flex;
				margin-bottom: 20rpx;
				.coupon-left{
					width: 240rpx;
					position: relative;
					.coupon-img{
						width: 240rpx;
						height: 170rpx;
					}
					.left-content{
						top: 0;
						left: 0;
						height: 100%;
						width: 100%;
						position: absolute;
						display: flex;
						align-items: center;
						justify-content: center;
					}
					.price{
						color: #fff;
						display: flex;
						align-items: baseline;
						.price-unit{
							font-size: 32rpx;
							line-height: 1;
							font-weight: bold;
						}
						.price-num{
							font-size: 56rpx;
							line-height: 1;
							font-weight: bold;
						}
					}
				}
				.coupon-right{
					flex: 1;
					height: 100%;
					display: flex;
					flex-direction: column;
					padding-left: 20rpx;
					background-color: #fff;
					.coupon-name{
						flex: 1;
						display: flex;
						align-items: center;
					}
					.only-coupon {
						margin-left: 12rpx;
						font-size: 24rpx;
						color: #999999;
					}
					.time{
						padding: 20rpx 20rpx 20rpx 0;
						display: flex;
						align-items: center;
						justify-content: space-between;
					}
					.time-left{
						color: #999;
						display: flex;
						font-size: 24rpx;
					}
					.time-status{
						color: #fff;
						width: 132rpx;
						height: 40rpx;
						font-size: 22rpx;
						border-radius: 100rpx;
						background-color: #cecece;
						display: flex;
						align-items: center;
						justify-content: center;
					}
					.time-status.active{
						background-color: $theme_color;
					}
				}
			}
		}
	}
</style>
